<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Validate;
use app\facade\Redis;
use think\Facade\Config as Configs;
use app\models\{Tels, Banner, GarbageCate, Holiday, CateSons, AreaPrice, Region, Order, Orderlog, Annous, Riderlist, User as Users, PartnerApply, RiderStreet, Graborder, IntergralLog, Address, Totaldata};

class Garbag extends Base
{
    public function user_edit()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            $validate = new Validate([
                'id' => 'require',
                'provinceid' => 'require',
                'cityid' => 'require',
                'areaid' => 'require',
                'street' => 'require',
                'name' => 'require',
                'mobile' => 'require',
                'address' => 'require',
                'nickname' => 'require',
                'image' => 'require',
                'dates' => 'require',
            ], [
                'id.require' => '用户id不能为空',
                'provinceid.require' => '省份id不能为空',
                'cityid.require' => '城市id不能为空',
                'areaid.require' => '区域id不能为空',
                'street.require' => '街道id不能为空',
                'name.require' => '姓名不能为空',
                'mobile.require' => '手机号不能为空',
                'address.require' => '详细地址不能为空',
                'nickname.require' => '昵称不能为空',
                'image.require' => '头像不能为空',
                'dates.require' => '日期不能为空'
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            $address = (new Address())->where(['user_id' => $data['id']])->order(['is_default' => 'desc'])->find();
            Db::startTrans();
            try {
                (new Users())->where('id', $data['id'])->update([
                    'nickname' => $data['nickname'],
                    'logo' => $data['image'],
                    'created' => $data['dates']
                ]);
                if (empty($address)) {
                    (new Address())->allowfield(true)->save([
                        'user_id' => $data['id'],
                        'name' => $data['name'],
                        'mobile' => $data['mobile'],
                        'province' => $data['provinceid'],
                        'city' => $data['cityid'],
                        'area' => $data['areaid'],
                        'street' => $data['street'],
                        'address' => $data['address'],
                    ]);
                } else {
                    (new Address())->where('id', $address['id'])->update([
                        'user_id' => $data['id'],
                        'name' => $data['name'],
                        'mobile' => $data['mobile'],
                        'province' => $data['provinceid'],
                        'city' => $data['cityid'],
                        'area' => $data['areaid'],
                        'street' => $data['street'],
                        'address' => $data['address'],
                    ]);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => $e->getMessage()]);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '操作成功']);
        } else {
            $id = $this->request->get('id');
            $row = (new Users())->where('id', $id)->find();
            $province = (new Region())->where(['level' => 1])->select();
            $city = [];
            $area = [];
            $street = [];
            $address = (new Address())->where(['user_id' => $id])->order(['is_default' => 'desc'])->find();
            if ($address) {
                $row['province'] = $address['province'];
                $row['city'] = $address['city'];
                $row['area'] = $address['area'];
                $row['street'] = $address['street'];
                $row['name'] = $address['name'];
                $row['mobile'] = $address['mobile'];
                $row['address'] = $address['address'];
                $cityid = (new Region())->where(['area_code' => $address['city'], 'level' => 2])->find();
                $city = (new Region())->where(['level' => 2, 'parent_id' => $cityid['parent_id']])->select();
                $areaid = (new Region())->where(['area_code' => $address['area'], 'level' => 3])->find();
                $area = (new Region())->where(['level' => 3, 'parent_id' => $areaid['parent_id']])->select();
                $streetid = (new Region())->where(['id' => $address['street'], 'level' => 4])->find();
                $street = (new Region())->where(['level' => 4, 'parent_id' => $streetid['parent_id']])->select();
            }
            return view('', ['row' => $row, 'province' => $province, 'city' => $city, 'area' => $area, 'street' => $street]);
        }
    }

    //添加修改
    public function user_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            $validate = new Validate([
                'provinceid' => 'require',
                'cityid' => 'require',
                'areaid' => 'require',
                'street' => 'require',
                'name' => 'require',
                'mobile' => 'require',
                'address' => 'require',
                'nickname' => 'require',
                'image' => 'require',
                'dates' => 'require',
            ], [
                'provinceid.require' => '省份id不能为空',
                'cityid.require' => '城市id不能为空',
                'areaid.require' => '区域id不能为空',
                'street.require' => '街道id不能为空',
                'name.require' => '姓名不能为空',
                'mobile.require' => '手机号不能为空',
                'address.require' => '详细地址不能为空',
                'nickname.require' => '昵称不能为空',
                'image.require' => '头像不能为空',
                'dates.require' => '日期不能为空'
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'data' => [], 'msg' => $validate->getError()]);
            }
            Db::startTrans();
            try {
                $user_id = (new Users())->insertGetId([
                    'openid' => md5(time() . rand(1, 100)),
                    'unionid' => md5(time() . rand(1, 100)),
                    'session_key' => md5(time() . rand(1, 100)),
                    'mobile' => '',
                    'nickname' => $data['nickname'],
                    'logo' => $data['image'],
                    'code' => recode(new Users()),
                    'issystem' => 2,
                    'created' => $data['dates']
                ]);
                $address = [
                    'user_id' => $user_id,
                    'name' => $data['name'],
                    'mobile' => $data['mobile'],
                    'province' => $data['provinceid'],
                    'city' => $data['cityid'],
                    'area' => $data['areaid'],
                    'street' => $data['street'],
                    'address' => $data['address'],
                ];
                (new Address())->allowfield(true)->save($address);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return json(['status' => 1, 'data' => [], 'msg' => '操作失败']);
            }
            return json(['status' => 0, 'data' => [], 'msg' => '操作成功']);
        } else {
            $province = (new Region())->where(['level' => 1])->select();
            return view('', ['province' => $province]);
        }
    }

    public function tels()
    {
        $ModelFun = new Tels();
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (!empty($data['province'])) {
            $list = $list->where(['provinceid' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $list = $list->where(['cityid' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $list = $list->where(['areaid' => $data['area']]);
        }
        if ($admin['type'] == 2) {
            $list = $list->where(['areaid' => $admin['area']]);
        }
        if ($admin['type'] == 3 || $admin['type'] == 4) {
            return view('', ['list' => []]);
        }
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $area = (new Region())->where(['area_code' => $lists['areaid'], 'level' => 3])->find();
            $city = (new Region())->where(['id' => $area['parent_id']])->find();
            $province = (new Region())->where(['id' => $city['parent_id']])->find();
            $lists['area_text'] = $area['name'];
            $lists['city_text'] = $city['name'];
            $lists['province_text'] = $province['name'];
        }
        return view('', ['list' => $list]);
    }

    //添加修改
    public function tels_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            if ($admin['type'] != 1 && $admin['type'] != 2) {
                return json(['status' => 1, 'msg' => '无操作权限']);
            }
            if ($admin['type'] == 2) {
                if (empty($data['areaid'])) {
                    return json(['status' => 1, 'msg' => '必须选择区域']);
                }
                if (!in_array($data['areaid'], $admin['area'])) {
                    return json(['status' => 1, 'msg' => '无权限操作该区域']);
                }
            }
            $tels = (new Tels())->where('areaid', $data['areaid'])->find();
            //查询该区域是否已经添加
            if (empty($data['id'])) {
                if (!empty($tels)) {
                    return json(['status' => 1, 'msg' => '该区域已添加联系电话']);
                }
                $result = (new Tels())->allowField(true)->save($data);
            } else {
                if (!empty($tels) && $tels['id'] != $data['id']) {
                    return json(['status' => 1, 'msg' => '该区域已添加联系电话']);
                }
                $result = (new Tels())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $admin = $this->request->admin;
            $id = $this->request->get('id');
            $row = (new Tels())->where('id', $id)->find();
            $province = (new Region())->where(['level' => 1])->select();
            $city = [];
            $area = [];
            if ($row) {
                $cityid = (new  Region())->where(['area_code' => $row['cityid'], 'level' => 2])->find();
                $city = (new Region())->where(['level' => 2, 'parent_id' => $cityid['parent_id']])->select();
                $area = (new Region())->where(['level' => 3, 'parent_id' => $cityid['id']])->select();
            }
            return view('', ['row' => $row, 'province' => $province, 'city' => $city, 'area' => $area, 'admin' => $admin]);
        }
    }

    public function tels_del()
    {
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        if ($admin['type'] != 1 && $admin['type'] != 2) {
            return json(['status' => 1, 'msg' => '无操作权限']);
        }
        $id = trim(input('id'), ',');
        if ($admin['type'] == 2) {
            $banner = (new Tels())->where('id', $id)->find();
            if (empty($banner) || !in_array($banner['areaid'], $admin['area'])) {
                return json(['status' => 1, 'msg' => '无权限操作该区域']);
            }
        }
        $result = (new Tels())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }


    public function user_del()
    {
        $admin = $this->request->admin;
        if ($admin['type'] != 1) {
            return json(['status' => 1, 'msg' => '无操作权限']);
        }
        $id = trim(input('id'), ',');
        $user = (new Users())->where('id', $id)->find();
        if (empty($user) || $user['issystem'] != 2) {
            return json(['status' => 1, 'msg' => '该用户不可删除']);
        }
        $result = (new Users())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }


    public function banner()
    {
        $ModelFun = new Banner();
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (!empty($data['province'])) {
            $list = $list->where(['provinceid' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $list = $list->where(['cityid' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $list = $list->where(['areaid' => $data['area']]);
        }
        if ($admin['type'] == 2) {
            $list = $list->where(['areaid' => $admin['area']]);
        }
        if ($admin['type'] == 3 || $admin['type'] == 4) {
            return view('', ['list' => []]);
        }
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $area = (new Region())->where(['area_code' => $lists['areaid'], 'level' => 3])->find();
            $city = (new Region())->where(['id' => $area['parent_id']])->find();
            $province = (new Region())->where(['id' => $city['parent_id']])->find();
            $lists['area_text'] = $area['name'];
            $lists['city_text'] = $city['name'];
            $lists['province_text'] = $province['name'];
        }
        return view('', ['list' => $list]);
    }

    //添加修改
    public function banner_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            if ($admin['type'] != 1 && $admin['type'] != 2) {
                return json(['status' => 1, 'msg' => '无操作权限']);
            }
            if ($admin['type'] == 2) {
                if (empty($data['areaid'])) {
                    return json(['status' => 1, 'msg' => '必须选择区域']);
                }
                if (!in_array($data['areaid'], $admin['area'])) {
                    return json(['status' => 1, 'msg' => '无权限操作该区域']);
                }
            }
            if (empty($data['id'])) {
                $result = (new Banner())->allowField(true)->save($data);
            } else {
                $result = (new Banner())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $admin = $this->request->admin;
            $id = $this->request->get('id');
            $row = (new Banner())->where('id', $id)->find();
            $province = (new Region())->where(['level' => 1])->select();
            $city = [];
            $area = [];
            if ($row) {
                $cityid = (new  Region())->where(['area_code' => $row['cityid'], 'level' => 2])->find();
                $city = (new Region())->where(['level' => 2, 'parent_id' => $cityid['parent_id']])->select();
                $area = (new Region())->where(['level' => 3, 'parent_id' => $cityid['id']])->select();
            }
            return view('', ['row' => $row, 'province' => $province, 'city' => $city, 'area' => $area, 'admin' => $admin]);
        }
    }

    public function banner_del()
    {
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        if ($admin['type'] != 1 && $admin['type'] != 2) {
            return json(['status' => 1, 'msg' => '无操作权限']);
        }
        $id = trim(input('id'), ',');
        if ($admin['type'] == 2) {
            $banner = (new Banner())->where('id', $id)->find();
            if (empty($banner) || !in_array($banner['areaid'], $admin['area'])) {
                return json(['status' => 1, 'msg' => '无权限操作该区域']);
            }
        }
        $result = (new Banner())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }

    public function holiday()
    {
        $data = $this->request->get();
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $model = new Holiday();
        $list = $model->order('id desc');
        if ($admin['type'] == 2) {
            $list = $list->where(['areaid' => $admin['area']]);
        } elseif ($admin['type'] > 2) {
            $list = $list->paginate(10, false, ['query' => $this->request->param()]);
            return view('', ['list' => $list]);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $area = (new Region())->where(['area_code' => $lists['areaid'], 'level' => 3])->find();
            $city = (new Region())->where(['area_code' => $lists['cityid'], 'level' => 2])->find();
            $province = (new Region())->where(['area_code' => $lists['provinceid'], 'level' => 1])->find();
            $lists['area_text'] = $area['name'];
            $lists['city_text'] = $city['name'];
            $lists['province_text'] = $province['name'];
        }
        return view('', ['list' => $list]);
    }


    //添加修改
    public function holiday_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['province']) || empty($data['city']) || empty($data['area'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '省市区参数不能为空']);
            }
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            $admin['street'] = explode(',', $admin['street']);
            if ($admin['type'] == 2) {
                if (!in_array($data['areaid'], $admin['area'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无该区域设置权限']);
                }
            }
            if ($admin['type'] == 3 || $admin['type'] == 4) {
                return json(['status' => 1, 'data' => [], 'msg' => '无该街道设置权限']);
            }
            $model = new Holiday();
            $arr = [
                'provinceid' => $data['province'],
                'cityid' => $data['city'],
                'areaid' => $data['area'],
                'msg' => $data['msg']
            ];
            if (!empty($data['id'])) {
                $haveholt = $model->where('areaid', $data['area'])->find();
                if (!empty($haveholt) && $haveholt['id'] != $data['id']) {
                    return json(['status' => 1, 'msg' => '区域已存在']);
                }
            }
            if (empty($data['id'])) {
                $result = $model->allowField(true)->save($arr);
            } else {
                $result = $model->allowField(true)->save($arr, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Holiday())->where('id', $id)->find();
            $province = (new Region())->where(['level' => 1])->select();
            $cateson = [];
            $city = [];
            $area = [];
            $street = [];
            if (!empty($row)) {
                $areaid = (new  Region())->where(['area_code' => $row['areaid'], 'level' => 3])->find();
                $cityid = (new  Region())->where(['id' => $areaid['parent_id'], 'level' => 2])->find();
                $provinceid = (new  Region())->where(['id' => $cityid['parent_id'], 'level' => 1])->find();
                $row['provinceid'] = $provinceid['area_code'];
                $row['cityid'] = $cityid['area_code'];
                $city = (new Region())->where(['level' => 2, 'parent_id' => $provinceid['id']])->select();
                $area = (new Region())->where(['level' => 3, 'parent_id' => $cityid['id']])->select();
            }
            return view('', ['row' => $row, 'province' => $province, 'city' => $city, 'area' => $area]);
        }
    }

    public function holiday_del()
    {
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        if ($admin['type'] != 1 && $admin['type'] != 2) {
            return json(['status' => 1, 'data' => [], 'msg' => '无操作权限']);
        }
        $id = trim(input('id'), ',');
        $row = (new Holiday())->where('id', $id)->find();
        if (empty($row)) {
            return json(['status' => 1, 'msg' => '信息错误，请重试']);
        }
        if ($admin['type'] == 2 && !in_array($row['areaid'], $admin['area'])) {
            return json(['status' => 1, 'data' => [], 'msg' => '无区域操作权限']);
        }
        $result = (new Holiday())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }


    public function rider_del()
    {
        $admin = $this->request->admin;
        if ($admin['type'] != 1) {
            return json(['status' => 1, 'data' => [], 'msg' => '合伙人请联系管理员进行删除']);
        }
        $id = trim(input('id'), ',');
        $row = (new Riderlist())->where('id', $id)->find();
        if (empty($row)) {
            return json(['status' => 1, 'msg' => '信息错误，请重试']);
        }
        Db::startTrans();
        try {
            (new Riderlist())->where('id', $id)->delete();
            (new RiderStreet())->where('riderid', $id)->delete();
            Db::commit();
        } catch (\Exception $e) {
            //dump($e->getMessage());
            Db::rollback();
            return json(['status' => 1, 'msg' => '操作失败']);
        }
        return json(['status' => 0, 'msg' => '操作成功']);
    }

    public function rider()
    {
        $ModelFun = new Riderlist();
        $data = $this->request->get();
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $list = $ModelFun->order('id desc');
        $model = new RiderStreet();
        if (!empty($data['street'])) {
            $model = $model->where('street', $data['street']);
        } elseif (!empty($data['area'])) {
            $model = $model->where('areaid', $data['area']);
        } elseif (!empty($data['city'])) {
            $model = $model->where('cityid', $data['city']);
        } elseif (!empty($data['province'])) {
            $model = $model->where('provinceid', $data['province']);
        }
        // if ($admin['type'] == 1) {
        //     $riderid = $model->column('riderid');
        //     if (count($riderid) > 0) {
        //         $list = $list->where(['id' => $riderid]);
        //     }
        // }
        if ($admin['type'] == 2) {
            $riderid = $model->where(['areaid' => $admin['area']])->column('riderid');
            if (count($riderid) > 0) {
                $list = $list->where(['id' => $riderid]);
            } else {
                $list = $list->where('id', 0);
            }
        }
        if ($admin['type'] == 3) {
            $riderid = $model->where(['street' => $admin['street']])->column('riderid');
            if (count($riderid) > 0) {
                $list = $list->where(['id' => $riderid]);
            } else {
                $list = $list->where('id', 0);
            }
        }
        if ($admin['type'] == 4) {
            $riderid = $model->where(['street' => $admin['street']])->column('riderid');
            if (count($riderid) > 0) {
                $list = $list->where(['id' => $riderid]);
            } else {
                $list = $list->where('id', 0);
            }
        }
        if (!empty($data['mobile'])) {
            $list = $list->where('mobile|cards', 'like', '%' . $data['mobile'] . '%');
        }
        if (!empty($data['nickname'])) {
            $user_id = (new Users())->where('nickname', $data['nickname'])->value('id');
            $list = $list->where('user_id', $user_id);
        }
        if (!empty($data['isexcel']) && $data['isexcel'] == 2) {
            $name = date('Y-m-d H:i:s') . '骑手信息导出';
            $header =   [
                "骑手ID", "骑手账号", "骑手昵称",  "骑手姓名", "骑手身份证号码",
                "骑手负责街道数", "注册时间", "成交总单", date('Y-m-d') . "日成交单",
                "成交总额", date('Y-m-d') . "日成交额", "成交总重（公斤）",
                date('Y-m-d') . "日成交重", "推荐用户数", "推荐订单数", "推荐订单总额"
            ];
            $excel = [];
            $list = $list->select();
        } else {
            $list = $list->paginate(10, false, ['query' => $this->request->param()]);
        }
        foreach ($list as $lists) {
            $lists['streets'] = (new RiderStreet())->where(['riderid' => $lists['id'], 'status' => 2])->count();
            if ($lists['user_id'] > 0) {
                $tjcount = (new Users())->where(['pid' => $lists['user_id']])->column('id');
                $lists['tjcount'] = count($tjcount);
                if ($lists['tjcount'] > 0) {
                    $orders = (new Order())->where(['user_id' => $tjcount, 'status' => 3])->select();
                    $lists['tjocount']  = count($orders);
                    if ($lists['tjocount'] > 0) {
                        $lists['tjototal'] = array_sum(array_column($orders->toArray(), 'total'));
                    }
                }
                $user = (new Users())->where(['id' => $lists['user_id']])->find();
                $lists['nickname'] = $user['nickname'];
                $lists['logo'] = $user['logo'];
            }
            $order = (new Order())->where(['orders_id' => $lists['id'], 'status' => 3])->column('ordersn');
            $todayorder = (new Order())->where(['orders_id' => $lists['id'], 'status' => 3, 'ordertime' => date('Y-m-d')])->column('ordersn');
            $lists['order'] = count($order);
            $lists['todayorder'] = count($todayorder);
            if ($lists['order'] > 0) {
                $lists['total'] = (new Orderlog())->where(['ordersn' => $order])->sum('total');
            }
            if ($lists['todayorder'] > 0) {
                $lists['todaytotal'] = (new Orderlog())->where(['ordersn' => $todayorder])->sum('total');
            }
            if ($lists['order'] > 0) {
                $lists['weight'] = (new Orderlog())->where(['ordersn' => $order])->sum('weight');
            }
            if ($lists['todayorder'] > 0) {
                $lists['todayweight'] = (new Orderlog())->where(['ordersn' => $todayorder])->sum('weight');
            }
            if (!empty($data['isexcel']) && $data['isexcel'] == 2) {
                $excel[] = [
                    $lists['id'],
                    $lists['mobile'],
                    $lists['nickname'] ?? '',
                    $lists['name'],
                    $lists['cards'],
                    $lists['streets'],
                    $lists['created'],
                    $lists['order'] ?? 0,
                    $lists['todayorder'] ?? 0,
                    $lists['total'] ?? 0,
                    $lists['todaytotal'] ?? 0,
                    $lists['weight'] ?? 0,
                    $lists['todayweight'] ?? 0,
                    $lists['tjcount'] ?? 0,
                    $lists['tjocount'] ?? 0,
                    $lists['tjototal'] ?? 0
                ];
            }
        }
        if (!empty($data['isexcel']) && $data['isexcel'] == 2) {
            excelExport($name, $header, $excel);
        }
        return view('', ['list' => $list]);
    }

    public function rider_area()
    {
        $data = input('get.');
        return view('', ['list' => $data]);
    }

    //添加修改
    public function rider_edit()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['id']) || empty($data['password'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '用户id和密码不能为空']);
            }
            if (!preg_match('/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z]+$/', $data['password'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '密码必须是字母+数字组合']);
            }
            $rider = (new Riderlist())->where(['id' => $data['id']])->find();
            if (empty($rider)) {
                return json(['status' => 1, 'data' => [], 'msg' => '骑手不存在']);
            }
            $riders = (new Riderlist())->where(['mobile' => $data['mobile']])->find();
            if (!empty($riders) && $riders['id'] != $data['id']) {
                return json(['status' => 1, 'data' => [], 'msg' => '手机号码已存在']);
            }
            //查看负责区域
            $riderstreet = (new RiderStreet())->where(['riderid' => $rider['id'], 'status' => 2])->select();
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            $admin['street'] = explode(',', $admin['street']);
            if ($admin['type'] == 2) {
                $city = 0;
                foreach ($riderstreet as $riderstreets) {
                    if (in_array($riderstreets['areaid'], $admin['area'])) {
                        $city += 1;
                    }
                }
                if ($city <= 0) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无设置权限']);
                }
            }
            if ($admin['type'] == 3) {
                $area = 0;
                foreach ($riderstreet as $riderstreets) {
                    if (in_array($riderstreets['street'], $admin['street'])) {
                        $area += 1;
                    }
                }
                if ($area <= 0) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无设置权限']);
                }
            }
            if ($admin['type'] == 4) {
                $street = 0;
                foreach ($riderstreet as $riderstreets) {
                    if (in_array($riderstreets['street'], $admin['street'])) {
                        $street += 1;
                    }
                }
                if ($street <= 0) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无设置权限']);
                }
            }
            $result = (new Riderlist())->where('id', $data['id'])->update(['password' => md5(md5($data['password']))]);
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Riderlist())->where(['id' => $id])->find();
            return view('', ['row' => $row]);
        }
    }
    //添加修改
    public function street_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['provinceid']) || empty($data['cityid']) || empty($data['areaid']) || empty($data['riderid']) || empty($data['street'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '省市区街道参数不能为空']);
            }
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            $admin['street'] = explode(',', $admin['street']);
            if ($admin['type'] == 2) {
                // $pantid = (new Region())->where(['area_code' => $admin['area'], 'level' => 3])->value('id');
                // $areas = (new Region())->where('parent_id', $pantid)->column('area_code');
                if (!in_array($data['areaid'], $admin['area'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无该区域设置权限']);
                }
            }
            if ($admin['type'] == 3 && !in_array($data['street'], $admin['street'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '无该街道设置权限']);
            }
            if ($admin['type'] == 4 && !in_array($data['street'], $admin['street'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '无该街道|村镇设置权限']);
            }
            $arr = [
                'provinceid' => $data['provinceid'],
                'cityid' => $data['cityid'],
                'areaid' => $data['areaid'],
                'street' => $data['street'],
                'riderid' => $data['riderid'],
                'status' => 2
            ];
            if (empty($data['id'])) {
                $catsons = (new RiderStreet())->where(['riderid' => $data['riderid'], 'street' => $data['street']])->find();
                if (!empty($catsons)) {
                    return json(['status' => 1, 'data' => [], 'msg' => '该区域分该骑手已管理']);
                }
            }
            $result = (new RiderStreet())->allowField(true)->save($arr);
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $province = (new Region())->where(['level' => 1])->select();
            return view('', ['province' => $province, 'id' => $id]);
        }
    }

    public function cate()
    {
        $ModelFun = new GarbageCate();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc')->paginate(10, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    //添加修改
    public function cate_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            $admin['street'] = explode(',', $admin['street']);
            if ($admin['type'] != 1) {
                return json(['status' => 1, 'data' => [], 'msg' => '无操作权限']);
            }
            $data = input('post.');
            if (empty($data['id'])) {
                $result = (new GarbageCate())->allowField(true)->save($data);
            } else {
                $result = (new GarbageCate())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new GarbageCate())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function cate_del()
    {
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        if ($admin['type'] != 1) {
            return json(['status' => 1, 'data' => [], 'msg' => '无操作权限']);
        }
        $id = trim(input('id'), ',');
        $result = (new GarbageCate())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }


    public function cateson()
    {
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $ModelFun = new CateSons();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (!empty($data['cate'])) {
            $list = $list->where('cate', $data['cate']);
        }
        if (!empty($data['title'])) {
            $list = $list->where('name', 'like', '%' . $data['title'] . '%');
        }
        if ($admin['type'] == 1) {
            $list = $list->paginate(10, false, ['query' => $this->request->param()]);
        } elseif ($admin['type'] == 2) {
            $admin['province'] = [$admin['province'], 0];
            $admin['city'] = [$admin['city'], 0];
            $admin['area'][] = 0;
            $list = $list->where(['provinceid' => $admin['province'], 'cityid' =>  $admin['city'], 'areaid' =>  $admin['area']])
                ->paginate(10, false, ['query' => $this->request->param()]);
        } elseif ($admin['type'] >= 3) {
            $admin['province'] = [$admin['province'], 0];
            $admin['city'] = [$admin['city'], 0];
            $admin['area'][] = 0;
            $admin['street'][] = 0;
            $list = $list->where(['provinceid' => $admin['province'], 'cityid' =>  $admin['city'], 'areaid' =>  $admin['area'], 'streetid' =>  $admin['street']])
                ->paginate(10, false, ['query' => $this->request->param()]);
        }
        $cate = (new GarbageCate())->select();
        foreach ($list as $lists) {
            $street = (new Region())->where(['id' => $lists['streetid'], 'level' => 4])->find();
            $area = (new Region())->where(['area_code' => $lists['areaid'], 'level' => 3])->find();
            $city = (new Region())->where(['area_code' => $lists['cityid'], 'level' => 2])->find();
            $province = (new Region())->where(['area_code' => $lists['provinceid'], 'level' => 1])->find();
            $lists['street_text'] = $street['name'];
            $lists['area_text'] = $area['name'];
            $lists['city_text'] = $city['name'];
            $lists['province_text'] = $province['name'];
        }
        return view('', ['list' => $list, 'cate' => $cate, 'admin' => $admin]);
    }

    //添加修改
    public function cateson_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            $admin['street'] = explode(',', $admin['street']);
            $data = $this->request->post();
            if (!empty($data['id']) && $admin['type'] > 1) {
                $sons = (new CateSons())->where('id', $data['id'])->find();
                if (!empty($sons) && $sons['provinceid'] == 0) {
                    return json(['status' => 1, 'data' => [], 'msg' => '系统分类无操作权限']);
                }
            }
            if (empty($data['cate']) || empty($data['name'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '分类、名称参数不能为空']);
            }
            if ((empty($data['province']) || empty($data['city']) || empty($data['area'])) && $admin['type'] >= 2) {
                return json(['status' => 1, 'data' => [], 'msg' => '省市区等信息不能为空']);
            }
            if ($admin['type'] == 2) {
                if (!in_array($data['area'], $admin['area'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '区域无权限设置']);
                }
            }
            if (($admin['type'] == 3 || $admin['type'] == 4) && empty($data['street'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '区域、街道合伙人只能设置街道分类']);
            }
            if ($admin['type'] == 3 || $admin['type'] == 4) {
                if (!in_array($data['street'], $admin['street'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '村镇、街道无权限设置']);
                }
            }
            //查省市区是否存在
            $province = (new Region())->where(['area_code' => $data['province'], 'level' => 1])->find();
            if (empty($province) && !empty($data['province'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '省份编码错误']);
            } elseif (empty($data['province'])) {
                $data['province'] = 0;
            }
            $city = (new Region())->where(['area_code' => $data['city'], 'level' => 2, 'parent_id' => $province['id']])->find();
            if (empty($city) && !empty($data['city'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '城市编码错误']);
            } elseif (empty($data['city'])) {
                $data['city'] = 0;
            }
            $area = (new Region())->where(['area_code' => $data['area'], 'level' => 3, 'parent_id' => $city['id']])->find();
            if (empty($area) && !empty($data['area'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '区域编码错误']);
            } elseif (empty($data['area'])) {
                $data['area'] = 0;
            }
            if (!empty($area['id']) && !empty($data['street'])) {
                $street = (new Region())->where(['id' => $data['street'], 'level' => 4, 'parent_id' => $area['id']])->find();
                if (empty($street)) {
                    return json(['status' => 1, 'data' => [], 'msg' => '街道编码错误']);
                }
            } elseif (empty($data['street'])) {
                $data['street'] = 0;
            }
            //判断信息是否已经存在了
            if (!empty($data['street'])) {
                $havecat = (new CateSons())
                    ->where(['cate' => $data['cate'], 'name' => $data['name'], 'provinceid' => $data['province'], 'cityid' => $data['city'], 'areaid' => $data['area'], 'streetid' => $data['street']])
                    ->find();
                if ((!empty($havecat) && !empty($data['id']) && $data['id'] != $havecat['id']) || (!empty($havecat) && empty($data['id']))) {
                    return json(['status' => 1, 'data' => [], 'msg' => '地区分类已存在无需重复添加街道分类']);
                }
            } else {
                $havecat = (new CateSons())
                    ->where(['cate' => $data['cate'], 'name' => $data['name'], 'provinceid' => $data['province'], 'cityid' => $data['city'], 'areaid' => $data['area'], 'streetid' => $data['street']])
                    ->find();
                if ((!empty($havecat) && !empty($data['id']) && $data['id'] != $havecat['id']) || (!empty($havecat) && empty($data['id']))) {
                    return json(['status' => 1, 'data' => [], 'msg' => '分类信息已存在']);
                }
            }
            if ($data['province'] > 0 && ($data['city'] == 0 || $data['area'] == 0)) {
                return json(['status' => 1, 'data' => [], 'msg' => '分类信息级别需细分到区域']);
            }
            $data['provinceid'] = $data['province'];
            $data['cityid'] = $data['city'];
            $data['areaid'] = $data['area'];
            $data['streetid'] = $data['street'];
            if (empty($data['id'])) {
                $result = (new CateSons())->allowField(true)->save($data);
            } else {
                $result = (new CateSons())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $cate = (new GarbageCate())->select();
            $row = (new CateSons())->where('id', $id)->find();
            $province = (new Region())->where(['level' => 1])->select();
            $cateson = [];
            $city = [];
            $area = [];
            $street = [];
            if (!empty($row)) {
                $streetid = (new  Region())->where(['id' => $row['streetid'], 'level' => 4])->find();
                $areaid = (new  Region())->where(['area_code' => $row['areaid'], 'level' => 3])->find();
                $cityid = (new  Region())->where(['id' => $areaid['parent_id'], 'level' => 2])->find();
                $provinceid = (new  Region())->where(['id' => $cityid['parent_id'], 'level' => 1])->find();
                $row['provinceid'] = $provinceid['area_code'];
                $row['cityid'] = $cityid['area_code'];
                $city = (new Region())->where(['level' => 2, 'parent_id' => $provinceid['id']])->select();
                $area = (new Region())->where(['level' => 3, 'parent_id' => $cityid['id']])->select();
                $street = (new Region())->where(['level' => 4, 'parent_id' => $areaid['id']])->select();
            }
            return view('', ['row' => $row, 'cate' => $cate, 'cateson' => $cateson, 'province' => $province, 'city' => $city, 'area' => $area, 'street' => $street]);
        }
    }

    public function cateson_del()
    {
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $id = trim(input('id'), ',');
        $catson = (new CateSons())->where('id', $id)->find();
        if (empty($catson)) {
            return json(['status' => 1, 'data' => [], 'msg' => '分类信息不存在']);
        }
        if ($catson['provinceid'] == 0) {
            return json(['status' => 1, 'data' => [], 'msg' => '系统分类不可删']);
        }
        if ($admin['type'] == 2 && !in_array($catson['areaid'], $admin['area'])) {
            return json(['status' => 1, 'data' => [], 'msg' => '无操作权限']);
        }
        if ($admin['type'] == 3 && !in_array($catson['streetid'], $admin['street'])) {
            return json(['status' => 1, 'data' => [], 'msg' => '无操作权限']);
        }
        $result = (new CateSons())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }


    public function areaprice()
    {
        $ModelFun = new AreaPrice();
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (!empty($data['cate']) && empty($data['cateson'])) {
            $cate = [];
            $cate = (new CateSons())->where(['cate' => $data['cate']])->column('id');
            $list = $list->where(['catsonsid' => $cate]);
        }
        if (!empty($data['cateson'])) {
            $list = $list->where('catsonsid', $data['cateson']);
        }
        if (!empty($data['province'])) {
            $list = $list->where(['provinceid' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $list = $list->where(['cityid' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $list = $list->where(['areaid' => $data['area']]);
        }
        if ($admin['type'] == 2) {
            $list = $list->where(['areaid' => $admin['area']]);
        }
        if ($admin['type'] == 3 || $admin['type'] == 4) {
            $list = $list->where(['streetid' => $admin['street']]);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $street = (new Region())->where(['id' => $lists['streetid'], 'level' => 4])->find();
            $area = (new Region())->where(['area_code' => $lists['areaid'], 'level' => 3])->find();
            $city = (new Region())->where(['id' => $area['parent_id']])->find();
            $province = (new Region())->where(['id' => $city['parent_id']])->find();
            $lists['street_text'] = $street['name'];
            $lists['area_text'] = $area['name'];
            $lists['city_text'] = $city['name'];
            $lists['province_text'] = $province['name'];
            $cateson = (new CateSons())->where('id', $lists['catsonsid'])->find();
            $lists['cateson_name'] = $cateson['name'];
            $lists['cate_name'] = (new GarbageCate())->where('id', $cateson['cate'])->value('name');
        }
        return view('', ['list' => $list, 'data' => $data]);
    }

    public function annous()
    {
        $ModelFun = new Annous();
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (!empty($data['province'])) {
            $list = $list->where(['provinceid' => $data['province']]);
        }
        if (!empty($data['city'])) {
            $list = $list->where(['cityid' => $data['city']]);
        }
        if (!empty($data['area'])) {
            $list = $list->where(['areaid' => $data['area']]);
        }
        if (!empty($data['title'])) {
            $list = $list->where('title', 'like', '%' . $data['title'] . '%');
        }
        if ($admin['type'] == 2 || $admin['type'] == 3) {
            $list = $list->where(['areaid' => $admin['area']]);
        }
        if ($admin['type'] == 4) {
            $list = $list->where(['streetid' => $admin['street']]);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $street = (new Region())->where(['id' => $lists['streetid'], 'level' => 4])->find();
            $area = (new Region())->where(['area_code' => $lists['areaid'], 'level' => 3])->find();
            $city = (new Region())->where(['id' => $area['parent_id']])->find();
            $province = (new Region())->where(['id' => $city['parent_id']])->find();
            $lists['street_text'] = $street['name'];
            $lists['area_text'] = $area['name'];
            $lists['city_text'] = $city['name'];
            $lists['province_text'] = $province['name'];
        }
        return view('', ['list' => $list, 'data' => $data]);
    }

    public function annous_del()
    {
        $rdslist = Redis::get('clickreturn');
        if ($rdslist) {
            return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
        }
        Redis::setex('clickreturn', 2, 1);
        $data = input('get.');
        $res = (new Annous())->where('id', $data['id'])->delete();
        if ($res) {
            return json(['status' => 0, 'data' => [], 'msg' => '操作完成']);
        }
        return json(['status' => 1, 'data' => [], 'msg' => '操作失败']);
    }

    //添加修改
    public function annous_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            $annous = (new Annous())->where('id', $data['id'])->find();
            if (!empty($data['id']) && empty($annous)) {
                return json(['status' => 1, 'data' => [], 'msg' => '公告信息不存在']);
            }
            if (!empty($data['id']) && $annous['provinceid'] > 0) {
                $data['province'] = $annous['provinceid'];
                $data['city'] = $annous['cityid'];
                $data['area'] = $annous['areaid'];
                if (empty($data['street'])) {
                    $data['street'] = $annous['streetid'];
                } else {
                    $street = (new Region())->where(['id' => $data['street'], 'level' => 4])->find();
                    if ($street['area_code'] != $data['area']) {
                        return json(['status' => 1, 'data' => [], 'msg' => '街道编码不匹配']);
                    }
                }
            } else {
                if (empty($data['title']) || empty($data['content']) || empty($data['province']) || empty($data['city']) || empty($data['area'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '省市区参数不能为空']);
                }
                //查省市区是否存在
                $province = (new Region())->where(['area_code' => $data['province'], 'level' => 1])->find();
                if (empty($province)) {
                    return json(['status' => 1, 'data' => [], 'msg' => '省份编码错误']);
                }
                $city = (new Region())->where(['area_code' => $data['city'], 'level' => 2, 'parent_id' => $province['id']])->find();
                if (empty($city)) {
                    return json(['status' => 1, 'data' => [], 'msg' => '城市编码错误']);
                }
                $area = (new Region())->where(['area_code' => $data['area'], 'level' => 3, 'parent_id' => $city['id']])->find();
                if (empty($area)) {
                    return json(['status' => 1, 'data' => [], 'msg' => '区域编码错误']);
                }
                if (!empty($data['street'])) {
                    $street = (new Region())->where(['id' => $data['street'], 'level' => 4, 'parent_id' => $area['id']])->find();
                    if (empty($street)) {
                        return json(['status' => 1, 'data' => [], 'msg' => '街道编码错误']);
                    }
                } else {
                    $data['street'] = 0;
                }
            }
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            $admin['street'] = explode(',', $admin['street']);
            if ($admin['type'] == 2) {
                if (!in_array($data['area'], $admin['area'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无权限设置']);
                }
            }
            if ($admin['type'] == 3) {
                if (!in_array($data['area'], $admin['area'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无权限设置']);
                }
            }
            if ($admin['type'] == 4) {
                if (!in_array($data['street'], $admin['street'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无权限设置']);
                }
            }
            $arr = [
                'title' => $data['title'],
                'content' => $data['content'],
                'provinceid' => $data['province'],
                'cityid' => $data['city'],
                'areaid' => $data['area'],
                'streetid' => $data['street'],
            ];
            if (empty($data['id'])) {
                if ($data['street'] > 0) {
                    $catsons = (new Annous())->where(['streetid' => $data['street']])->find();
                    if (!empty($catsons)) {
                        return json(['status' => 1, 'data' => [], 'msg' => '该街道公告已设置']);
                    }
                } else {
                    $catsons = (new Annous())->where(['areaid' => $data['area'], 'streetid' => 0])->find();
                    if (!empty($catsons)) {
                        return json(['status' => 1, 'data' => [], 'msg' => '该区域公告已设置']);
                    }
                }
            } else {
                if ($data['street'] > 0) {
                    $catsons = (new Annous())->where(['streetid' => $data['street']])->find();
                    if (!empty($catsons) && $catsons['id'] != $data['id']) {
                        return json(['status' => 1, 'data' => [], 'msg' => '该街道公告已设置']);
                    }
                } else {
                    $catsons = (new Annous())->where(['areaid' => $data['area'], 'streetid' => 0])->find();
                    if (!empty($catsons) && $catsons['id'] != $data['id']) {
                        return json(['status' => 1, 'data' => [], 'msg' => '该区域公告已设置']);
                    }
                }
            }
            if (empty($data['id'])) {
                $result = (new Annous())->allowField(true)->save($arr);
            } else {
                $result = (new Annous())->allowField(true)->save($arr, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Annous())->where('id', $id)->find();
            $province = (new Region())->where(['level' => 1])->select();
            $city = [];
            $area = [];
            $street = [];
            if (!empty($row)) {
                $areaid = (new  Region())->where(['area_code' => $row['areaid'], 'level' => 3])->find();
                $cityid = (new  Region())->where(['id' => $areaid['parent_id'], 'level' => 2])->find();
                $provinceid = (new  Region())->where(['id' => $cityid['parent_id'], 'level' => 1])->find();
                $row['provinceid'] = $provinceid['area_code'];
                $row['cityid'] = $cityid['area_code'];
                $city = (new Region())->where(['level' => 2, 'parent_id' => $provinceid['id']])->select();
                $area = (new Region())->where(['level' => 3, 'parent_id' => $cityid['id']])->select();
                $street = (new Region())->where(['level' => 4, 'parent_id' => $areaid['id']])->select();
            }
            return view('', ['row' => $row, 'province' => $province, 'city' => $city, 'area' => $area, 'street' => $street]);
        }
    }

    //添加修改
    public function areaprice_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['cateson'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '请选择子分类']);
            }
            $cateson = (new CateSons())->where('id', $data['cateson'])->find();
            if (empty($cateson)) {
                return json(['status' => 1, 'data' => [], 'msg' => '分类不存在']);
            }
            if ($cateson['provinceid'] > 0) {
                if (empty($data['price'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '价格不能为0']);
                }
                $data['province'] = $cateson['provinceid'];
                $data['city'] = $cateson['cityid'];
                $data['area'] = $cateson['areaid'];
                if (empty($data['street'])) {
                    $data['street'] = $cateson['streetid'];
                } else {
                    $street = (new Region())->where(['id' => $data['street'], 'level' => 4])->find();
                    if ($street['area_code'] != $data['area']) {
                        return json(['status' => 1, 'data' => [], 'msg' => '街道编码不匹配']);
                    }
                }
            } else {
                if (empty($data['price']) || empty($data['cateson']) || empty($data['province']) || empty($data['city']) || empty($data['area'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '参数不能为空']);
                }
                //查省市区是否存在
                $province = (new Region())->where(['area_code' => $data['province'], 'level' => 1])->find();
                if (empty($province)) {
                    return json(['status' => 1, 'data' => [], 'msg' => '省份编码错误']);
                }
                $city = (new Region())->where(['area_code' => $data['city'], 'level' => 2, 'parent_id' => $province['id']])->find();
                if (empty($city)) {
                    return json(['status' => 1, 'data' => [], 'msg' => '城市编码错误']);
                }
                $area = (new Region())->where(['area_code' => $data['area'], 'level' => 3, 'parent_id' => $city['id']])->find();
                if (empty($area)) {
                    return json(['status' => 1, 'data' => [], 'msg' => '区域编码错误']);
                }
                if (!empty($data['street'])) {
                    $street = (new Region())->where(['id' => $data['street'], 'level' => 4, 'parent_id' => $area['id']])->find();
                    if (empty($street)) {
                        return json(['status' => 1, 'data' => [], 'msg' => '街道编码错误']);
                    }
                } else {
                    $data['street'] = 0;
                }
            }
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            $admin['street'] = explode(',', $admin['street']);
            if ($admin['type'] == 2) {
                if (!in_array($data['area'], $admin['area'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无权限设置']);
                }
            }
            if ($admin['type'] == 3) {
                if (!in_array($data['street'], $admin['street'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无权限设置']);
                }
            }
            if ($admin['type'] == 4) {
                if (!in_array($data['street'], $admin['street'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无权限设置']);
                }
            }
            $arr = [
                'price' => $data['price'],
                'catsonsid' => $data['cateson'],
                'provinceid' => $data['province'],
                'cityid' => $data['city'],
                'areaid' => $data['area'],
                'streetid' => $data['street'],
                'unmit' => $data['unmit'],
            ];
            if (empty($data['id'])) {
                if ($data['street'] > 0) {
                    $catsons = (new AreaPrice())->where(['catsonsid' => $data['cateson'], 'streetid' => $data['street']])->find();
                    if (!empty($catsons)) {
                        return json(['status' => 1, 'data' => [], 'msg' => '该街道分类价格已设置']);
                    }
                } else {
                    $catsons = (new AreaPrice())->where(['catsonsid' => $data['cateson'], 'areaid' => $data['area'], 'streetid' => 0])->find();
                    if (!empty($catsons)) {
                        return json(['status' => 1, 'data' => [], 'msg' => '该区域分类价格已设置']);
                    }
                }
            } else {
                if ($data['street'] > 0) {
                    $catsons = (new AreaPrice())->where(['catsonsid' => $data['cateson'], 'streetid' => $data['street']])->find();
                    if (!empty($catsons) && $catsons['id'] != $data['id']) {
                        return json(['status' => 1, 'data' => [], 'msg' => '该街道分类价格已设置']);
                    }
                } else {
                    $catsons = (new AreaPrice())->where(['catsonsid' => $data['cateson'], 'areaid' => $data['area'], 'streetid' => 0])->find();
                    if (!empty($catsons) && $catsons['id'] != $data['id']) {
                        return json(['status' => 1, 'data' => [], 'msg' => '该区域分类价格已设置']);
                    }
                }
            }
            if (empty($data['id'])) {
                $result = (new AreaPrice())->allowField(true)->save($arr);
            } else {
                $result = (new AreaPrice())->allowField(true)->save($arr, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new AreaPrice())->where('id', $id)->find();
            $cate = (new GarbageCate())->select();
            $province = (new Region())->where(['level' => 1])->select();
            $cateson = [];
            $city = [];
            $area = [];
            $street = [];
            if (!empty($row)) {
                $row['cate'] = (new CateSons())->where('id', $row['catsonsid'])->value('cate');
                $cateson = (new CateSons())->where('cate', $row['cate'])->select();
                $streetid = (new  Region())->where(['id' => $row['streetid'], 'level' => 4])->find();
                $areaid = (new  Region())->where(['area_code' => $row['areaid'], 'level' => 3])->find();
                $cityid = (new  Region())->where(['id' => $areaid['parent_id'], 'level' => 2])->find();
                $provinceid = (new  Region())->where(['id' => $cityid['parent_id'], 'level' => 1])->find();
                $row['provinceid'] = $provinceid['area_code'];
                $row['cityid'] = $cityid['area_code'];
                $city = (new Region())->where(['level' => 2, 'parent_id' => $provinceid['id']])->select();
                $area = (new Region())->where(['level' => 3, 'parent_id' => $cityid['id']])->select();
                $street = (new Region())->where(['level' => 4, 'parent_id' => $areaid['id']])->select();
            }
            return view('', ['row' => $row, 'cate' => $cate, 'cateson' => $cateson, 'province' => $province, 'city' => $city, 'area' => $area, 'street' => $street]);
        }
    }

    public function order()
    {
        $model = new Order();
        $data = $this->request->get();
        $list = $model->order('id desc');
        if (!empty($data['province'])) {
            $list = $list->where('province', $data['province']);
        }
        if (!empty($data['city'])) {
            $list = $list->where('city', $data['city']);
        }
        if (!empty($data['area'])) {
            $list = $list->where('area', $data['area']);
        }
        if (!empty($data['street'])) {
            $list = $list->where('street', $data['street']);
        }
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        if ($admin['type'] == 2) {
            $list = $list->where(['area' => $admin['area']]);
        }
        if ($admin['type'] == 3) {
            $list = $list->where(['street' => $admin['street']]);
        }
        if ($admin['type'] == 4) {
            $list = $list->where(['street' => $admin['street']]);
        }
        if (!empty($data['ordersn'])) {
            $list = $list->where('ordersn', $data['ordersn']);
        }
        if (!empty($data['status'])) {
            $list = $list->where('status', $data['status']);
        }
        if (!empty($data['cmobile'])) {
            $orders_id = (new Riderlist())->where('id|mobile', $data['cmobile'])->value('id');
            $list = $list->where('orders_id', $orders_id);
        }
        if (!empty($data['starttime'])) {
            $list = $list->where('ordertime', '>=', $data['starttime']);
        }
        if (!empty($data['endtime'])) {
            $list = $list->where('ordertime', '<', $data['endtime']);
        }
        if (!empty($data['mobile'])) {
            $mobile = (new Users())->where('id|mobile|nickname', $data['mobile'])->value('id');
            $list = $list->where('user_id', $mobile);
        }
        if (!empty($data['pid'])) {
            $tjcount = (new Users())->where(['pid' => $data['pid']])->column('id');
            if (count($tjcount) == 0) {
                $tjcount[] = -1;
            }
            $list = $list->where(['user_id' => $tjcount]);
        }
        $excel = [];
        if (!empty($data['isexcel']) && $data['isexcel'] == 2) {
            $name = date('Y-m-d H:i:s') . '订单信息导出';
            $header =   [
                "订单号",
                "用户昵称",
                "用户姓名",
                "用户联系方式",
                "接单骑手手机",
                "预约时间",
                "下单街道",
                "具体地址",
                "订单状态",
                "成交金额",
                "成交重量",
                "备注信息",
                "推荐骑手账号",
            ];
            $list = $list->select();
        } else {
            $list = $list->paginate(10, false, ['query' => $this->request->param()]);
        }
        foreach ($list as $result) {
            $user = (new Users())->where('id', $result['user_id'])->find();
            $result['weight'] = (new Orderlog())->where('ordersn', $result['ordersn'])->sum('weight');
            $result['usermobile'] = $user['mobile'];
            $result['nickname'] = $user['nickname'];
            $result['logo'] = $user['logo'];
            $riderlist = (new Riderlist())->where('id', $result['orders_id'])->find();
            $result['recmobile'] = $riderlist['mobile'];
            $result['riderlon'] = $riderlist['riderlon'];
            $result['riderlat'] = $riderlist['riderlat'];
            $result['province_text'] = (new Region())->where(['area_code' => $result['province'], 'level' => 1])->value('name');
            $result['city_text'] = (new Region())->where(['area_code' => $result['city'], 'level' => 2])->value('name');
            $result['area_text'] = (new Region())->where(['area_code' => $result['area'], 'level' => 3])->value('name');
            $result['street_text'] = (new Region())->where(['id' => $result['street'], 'level' => 4])->value('name');
            if (!empty($data['isexcel']) && $data['isexcel'] == 2) {
                if ($result['status'] == 2) {
                    $status = '已接单';
                } elseif ($result['status'] == 3) {
                    $status = '已完成';
                } else {
                    $status = '待接单';
                }
                if ($user['pid'] > 0) {
                    $result['tjmobile'] = (new Riderlist())->where('user_id', $user['pid'])->value('mobile');
                }
                $excel[] = [
                    $result['ordersn'],
                    $result['nickname'],
                    $result['name'],
                    $result['mobile'],
                    $result['recmobile'],
                    $result['appointment'],
                    $result['province_text'] . $result['city_text'] . $result['area_text'] . $result['street_text'],
                    $result['address'],
                    $status,
                    $result['total'],
                    $result['weight'],
                    $result['info'],
                    $result['tjmobile'] ?? '无',
                ];
            }
        }
        if (!empty($data['isexcel']) && $data['isexcel'] == 2) {
            excelExport($name, $header, $excel);
        }
        return view('', ['list' => $list]);
    }

    public function orderdetail()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $model = new Orderlog();
            $arr = [];
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            $admin['street'] = explode(',', $admin['street']);
            if ($admin['type'] == 2) {
                $order = (new Order())->where(['ordersn' => $data['ordersn'], 'city' => $admin['city']])->find();
            } elseif ($admin['type'] == 3) {
                $order = (new Order())->where(['ordersn' => $data['ordersn'], 'area' => $admin['area']])->find();
            } elseif ($admin['type'] == 4) {
                $order = (new Order())->where(['ordersn' => $data['ordersn'], 'street' => $admin['street']])->find();
            } else {
                $order = (new Order())->where('ordersn', $data['ordersn'])->find();
            }
            if (empty($order)) {
                return json(['status' => 1, 'msg' => '订单不存在或无权限']);
            }
            if ($order['status'] == 3) {
                return json(['status' => 1, 'msg' => '订单已完结']);
            }
            $total = 0;
            $weight = 0;
            foreach ($data['data'] as $datas) {
                if (!isset($datas['id']) || !isset($datas['price']) || !isset($datas['weight']) || !isset($datas['total'])) {
                    return json(['status' => 1, 'msg' => '参数不全']);
                }
                $savearr[] = [
                    'id' => $datas['id'],
                    'weight' => $datas['weight'],
                    'price' => $datas['price'],
                    'total' => round($datas['weight'] * $datas['price'], 2)
                ];
                $datas['total'] = round($datas['weight'] * $datas['price'], 2);
                $total += $datas['total'];
                $weight += $datas['weight'];
            }
            if (empty($savearr)) {
                return json(['status' => 1, 'msg' => '参数为空']);
            }
            $ordercount = Redis::zScore('ordercount', $order['orders_id']); //获取订单总量
            $ordertotal = Redis::zScore('ordertotal', $order['orders_id']); //获取订单总额
            $user = (new Users())->where('id', $order['user_id'])->find();
            Db::startTrans();
            try {
                (new Order())->where(['id' => $order['id']])->update(['status' => 3, 'total' => $total, 'weight' => $weight, 'endtime' => date('Y-m-d H:i:s')]);
                (new Orderlog())->allowfield(true)->saveAll($savearr);
                //积分赠送
                if ($weight > 0) {
                    if ($order['user_id'] == 956) {
                        $rate = 2;
                    } else {
                        $rate = (new Exchange())->where('area', $order['area'])->value('scale') ?? 1;
                    }
                    (new Users())->IntergralEdit($user['id'], $user['integral'], round($weight * $rate, 2), 1, $order['id'], '下单赠送(后台完结)');
                }
                if (empty($ordercount)) {
                    Redis::zAdd('ordercount', 1, $order['orders_id']);
                } else {
                    Redis::zIncrBy('ordercount', 1, $order['orders_id']);
                }
                if (empty($ordertotal)) {
                    Redis::zAdd('ordertotal', $total, $order['orders_id']);
                } else {
                    Redis::zIncrBy('ordertotal', $total, $order['orders_id']);
                }
                Db::commit();
            } catch (\Exception $e) {
                dump($e->getMessage());
                Db::rollback();
                return json(['status' => 1, 'msg' => '参数为空']);
            }
            return json(['status' => 0, 'msg' => '更新成功']);
        } else {
            $ordersn = $this->request->get('ordersn/d', 0);
            $model = new Orderlog();
            $row = $model->where('ordersn', $ordersn)->where(['del' => [1, 2]])->select();
            foreach ($row  as $rows) {
                $rows['type_text'] =  Configs::get('config.ordertype.' . $rows['type'])['name'];
            }
            return view('', ['row' => $row]);
        }
    }

    public function order_str()
    {
        $data = $this->request->post();
        $model = new Orderlog();
        $arr = [];
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        if ($admin['type'] == 2) {
            $order = (new Order())->where(['id' => $data['id'], 'area' => $admin['area']])->find();
        } elseif ($admin['type'] == 3) {
            $order = (new Order())->where(['id' => $data['id'], 'street' => $admin['street']])->find();
        } elseif ($admin['type'] == 4) {
            $order = (new Order())->where(['id' => $data['id'], 'street' => $admin['street']])->find();
        } else {
            $order = (new Order())->where('id', $data['id'])->find();
        }
        if (empty($order)) {
            return json(['code' => 1, 'msg' => '订单不存在或无权限']);
        }
        if ($order['status'] == 3 || $order['status'] == 4) {
            return json(['code' => 1, 'msg' => '订单状态无法操作']);
        }
        if ($data['status'] == 4) {
            $res = (new Order())->where('id', $data['id'])->update(['status' => 4]);
            if ($res) {
                return json(['code' => 0, 'msg' => '操作成功']);
            }
            return json(['code' => 0, 'msg' => '操作失败']);
        } else {
            //查找骑手存在否
            $rider = (new Riderlist())->where('mobile', $data['mobile'])->find();
            if (empty($rider)) {
                return json(['code' => 1, 'msg' => '骑手信息不存在']);
            }
            //判断骑手是否负责该街道
            $riderstreet = (new RiderStreet())->where(['riderid' => $rider['id'], 'status' => 2, 'street' => $order['street']])->find();
            if (empty($riderstreet)) {
                return json(['code' => 1, 'msg' => '该骑手不负责该街道']);
            }
            if ($rider['id'] == $order['orders_id']) {
                return json(['code' => 1, 'msg' => '骑手未发生更改']);
            }
            Db::startTrans();
            try {
                (new Order())->where(['id' => $order['id']])->update(['status' => 2, 'orders_id' => $rider['id'], 'ordertime' => date('Y-m-d')]);
                (new Graborder())->insert(['user_id' => $rider['id'], 'comeuser_id' => $order['orders_id'], 'orderid' => $order['id'], 'type' => 3]);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return json(['code' => 1, 'msg' => '操作失败，请重试']);
            }
            return json(['code' => 0, 'msg' => '操作成功']);
        }
    }

    public function parapply()
    {
        $model = new PartnerApply();
        $data = $this->request->get();
        $list = $model->order('id desc');
        if (!empty($data['province'])) {
            $list = $list->where('province', $data['province']);
        }
        if (!empty($data['city'])) {
            $list = $list->where('city', $data['city']);
        }
        if (!empty($data['area'])) {
            $list = $list->where('area', $data['area']);
        }
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        if ($admin['type'] == 2) {
            $list = $list->where(['area' => $admin['area']]);
        }
        if ($admin['type'] == 3) {
            $list = $list->where(['area' => []]);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()])->each(function ($result) {
            $result['usermobile'] = (new Users())->where('id', $result['user_id'])->value('mobile');
            $result['province_text'] = (new Region())->where(['area_code' => $result['province'], 'level' => 1])->value('name');
            $result['city_text'] = (new Region())->where(['area_code' => $result['city'], 'level' => 2])->value('name');
            $result['area_text'] = (new Region())->where(['area_code' => $result['area'], 'level' => 3])->value('name');
            return $result;
        });
        return view('', ['list' => $list]);
    }


    public function parapply_str()
    {
        $data = $this->request->post();
        if (empty($data['id']) || empty($data['status'])) {
            return json(['code' => 1, 'msg' => '参数错误']);
        }
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $result = (new PartnerApply())->where(['id' => $data['id'], 'status' => 1]);
        if ($admin['type'] == 2) {
            $result = $result->where(['area' => $admin['area']]);
        }
        if ($admin['type'] == 3) {
            $result = $result->where(['street' => $admin['street']]);
        }
        if ($admin['type'] == 4) {
            return json(['code' => 1, 'msg' => '无操作权限']);
        }
        $result = $result->find();
        if (empty($result)) {
            return json(['code' => 1, 'msg' => '申请不存在或您无权限']);
        }
        //判断是否已存在该区域城市合伙人
        if ($result['type'] == 1 && $data['status'] == 2) {
            $havecity = (new Users())->where(['cityid' => $result['city']])->find();
            if (!empty($havecity)) {
                return json(['code' => 1, 'msg' => '改地区存在城市合伙人，请选择驳回']);
            }
        }
        if ($data['status'] == 2) {
            $havecity = (new Users())->where(['areaid' => $result['area']])->find();
            if (!empty($havecity)) {
                return json(['code' => 1, 'msg' => '改地区区域合伙人存在，请选择驳回']);
            }
        }
        Db::startTrans();
        try {
            (new PartnerApply())->where(['id' => $data['id'], 'status' => 1])->update(['status' => $data['status']]);
            if ($data['status'] == 2 && $result['type'] == 1) {
                (new Users())->where(['id' => $result['user_id']])->update(['cityid' => $result['city'], 'areaid' => $result['area']]);
            }
            if ($data['status'] == 2 && $result['type'] == 2) {
                (new Users())->where(['id' => $result['user_id']])->update(['areaid' => $result['area']]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code' => 1, 'msg' => '操作失败，请重试']);
        }
        return json(['code' => 0, 'msg' => '操作成功']);
    }

    public function rider_street()
    {
        $model = new RiderStreet();
        $data = $this->request->get();
        $list = $model->alias('r');
        if (!empty($data['status'])) {
            $list = $list->where('r.status', $data['status']);
        }
        if (!empty($data['province'])) {
            $list = $list->where('r.provinceid', $data['province']);
        }
        if (!empty($data['city'])) {
            $list = $list->where('r.cityid', $data['city']);
        }
        if (!empty($data['area'])) {
            $list = $list->where('r.areaid', $data['area']);
        }
        if (!empty($data['street'])) {
            $list = $list->where('r.street', $data['street']);
        }
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        if (!empty($data['mobile'])) {
            $list = $list->where('rl.mobile|rl.name|rl.cards', $data['mobile']);
        }
        if ($admin['type'] == 2) {
            $list = $list->where(['r.areaid' => $admin['area']]);
        }
        if ($admin['type'] == 3) {
            $list = $list->where(['r.street' => $admin['street']]);
        }
        if ($admin['type'] == 4) {
            $list = $list->where(['r.street' => $admin['street']]);
        }
        $list = $list->order('r.id desc')->join('riderlist rl', 'r.riderid = rl.id', 'left')->field('r.*,rl.mobile,rl.name,rl.cards,rl.positive,rl.negative,rl.status as isstatus');
        $list = $list->paginate(10, false, ['query' => $this->request->param()])->each(function ($result) {
            $result['province_text'] = (new Region())->where(['area_code' => $result['provinceid'], 'level' => 1])->value('name');
            $result['city_text'] = (new Region())->where(['area_code' => $result['cityid'], 'level' => 2])->value('name');
            $result['area_text'] = (new Region())->where(['area_code' => $result['areaid'], 'level' => 3])->value('name');
            $result['street_text'] = (new Region())->where(['id' => $result['street'], 'level' => 4])->value('name');
            return $result;
        });
        return view('', ['list' => $list]);
    }

    public function rider_streetstr()
    {
        $data = $this->request->post();
        if (empty($data['id']) || empty($data['status'])) {
            return json(['code' => 1, 'msg' => '参数错误']);
        }
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $result = (new RiderStreet())->where(['id' => $data['id']]);
        if ($admin['type'] == 2) {
            $result = $result->where(['areaid' => $admin['area']]);
        }
        if ($admin['type'] == 3) {
            $result = $result->where(['street' => $admin['street']]);
        }
        if ($admin['type'] == 4) {
            $result = $result->where(['street' => $admin['street']]);
        }
        $result = $result->find();
        if (empty($result)) {
            return json(['code' => 1, 'msg' => '订单不存在或您无权限']);
        }
        if ($data['status'] == 4) {
            $res = (new RiderStreet())->where(['id' => $data['id']])->delete();
        } else {
            $res = (new RiderStreet())->where(['id' => $data['id']])->update(['status' => $data['status']]);
        }
        if ($res) {
            return json(['code' => 0, 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'msg' => '操作失败，请重试']);
    }

    public function user()
    {
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $model = new Users();
        $data = $this->request->get();
        $list = $model->order('id desc');
        if (!empty($data['title'])) {
            $list = $list->where('id|nickname|mobile', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['starttime'])) {
            $list = $list->where('created', '>=', $data['starttime']);
        }
        if (!empty($data['endtime'])) {
            $list = $list->where('created', '<', $data['endtime']);
        }
        if ($admin['type'] == 2) {
            $auser = (new Address())->where(['area' => $admin['area']])->column('user_id');
            $list = $list->where(['id' => $auser]);
        }
        if ($admin['type'] == 3 || $admin['type'] == 4) {
            $auser = (new Address())->where(['street' => $admin['street']])->column('user_id');
            $list = $list->where(['id' => $auser]);
        }
        if (!empty($data['pid'])) {
            $list = $list->where('pid', $data['pid']);
        }
        if (!empty($data['province'])) {
            $province = (new Address())->where('province', $data['province'])->column('user_id');
            if (count($province) > 0) {
                $list = $list->where(['id' => $province]);
            }
        }
        if (!empty($data['city'])) {
            $city = (new Address())->where('city', $data['city'])->column('user_id');
            if (count($city) > 0) {
                $list = $list->where(['id' => $city]);
            }
        }
        if (!empty($data['area'])) {
            $area = (new Address())->where('area', $data['area'])->column('user_id');
            if (count($area) > 0) {
                $list = $list->where(['id' => $area]);
            }
        }
        if (!empty($data['street'])) {
            $street = (new Address())->where('street', $data['street'])->column('user_id');
            if (count($street) > 0) {
                $list = $list->where(['id' => $street]);
            }
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $order = (new Order())->where(['user_id' => $lists['id'], 'status' => 3])->select();
            $lists['count'] = count($order);
            if ($lists['count'] > 0) {
                $lists['total'] = array_sum(array_column($order->toArray(), 'total'));
            }
            $address = (new Address())->order('is_default desc')->where('user_id', $lists['id'])->find();
            if (!empty($address)) {
                $address['province_text'] = (new Region())->where(['area_code' => $address['province']])->value('name');
                $address['city_text'] = (new Region())->where(['area_code' => $address['city']])->value('name');
                $address['area_text'] = (new Region())->where(['area_code' => $address['area']])->value('name');
                $address['street_text'] = (new Region())->where(['id' => $address['street']])->value('name');
            }
            $lists['address'] = $address;
        }
        return view('', ['list' => $list]);
    }

    public function integrallog()
    {
        $model = new IntergralLog();
        $id = $this->request->get('id/d', 0);
        $list = $model->where('user_id', $id)->order('id desc')->paginate(10, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    public function datas_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['provinceid']) || empty($data['cityid']) || empty($data['numbers']) || empty($data['areaid']) || empty($data['street']) || empty($data['address']) || empty($data['time'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '信息参数不能为空']);
            }
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            $admin['street'] = explode(',', $admin['street']);
            if ($admin['type'] == 2) {
                if (!in_array($data['areaid'], $admin['area'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无权限设置']);
                }
            }
            if ($admin['type'] == 3) {
                if (!in_array($data['street'], $admin['street'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无权限设置']);
                }
            }
            if ($admin['type'] == 4) {
                if (!in_array($data['street'], $admin['street'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '无权限设置']);
                }
                return json(['status' => 1, 'data' => [], 'msg' => '无操作权限']);
            }
            if (empty($data['price']) || empty($data['weight'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '重量|价格信息不能为空']);
            }
            $areaprice = (new AreaPrice())->where('id', $data['price'])->find();
            if (empty($data['price']) || empty($data['weight'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '区域价格不存在']);
            }
            $CateSons = (new CateSons())->where('id', $areaprice['catsonsid'])->find();
            $total = $areaprice['price'] * $data['weight'];
            $numbers = (int)$data['numbers'];
            $orderdata = [];
            $orderlog = [];
            $ordersn = time() . rand(11111, 99999);
            for ($i = 0; $i < $numbers; $i++) {
                $ordersn = $ordersn + $i;
                $orderdata = [
                    'user_id' => 0,
                    'appointment' => $data['time'],
                    'name' => $data['names'],
                    'mobile' => $data['phone'],
                    'province' => $data['provinceid'],
                    'city' =>  $data['cityid'],
                    'area' =>  $data['areaid'],
                    'street' =>  $data['street'],
                    'address' =>  $data['address'],
                    'lon' =>  0,
                    'lat' =>  0,
                    'status' => 3,
                    'ordersn' => $ordersn,
                    'info' => '',
                    'weight' => $data['weight'],
                    'total' => $total,
                    'ordertime' => $data['time'],
                    'message' => 2,
                    'issystem' => 2
                ];
                $orderlog = [
                    'ordersn' => $ordersn,
                    'areapriceid' => $areaprice['id'],
                    'money' =>  $areaprice['price'],
                    'type' => 2,
                    'icon' => $CateSons['icon'],
                    'name' => $CateSons['name'],
                    'money' =>  $areaprice['price'],
                    'weight' => $data['weight'],
                    'unmit' => $areaprice['unmit'],
                    'del' => 4,
                    'total' => $total
                ];
                Db::startTrans();
                try {
                    (new Order())->insert($orderdata);
                    (new Orderlog())->insert($orderlog);
                    Db::commit();
                } catch (\Exception $exception) {
                    dump($exception->getMessage());
                    Db::rollback();
                    return json(['status' => 1, 'data' => [], 'msg' => '第' . ($i + 1) . '操作失败,进程终止']);
                }
            }
            return json(['status' => 0, 'data' => [], 'msg' => '操作完成']);
        } else {
            return view('', []);
        }
    }

    public function areaprice_del()
    {
        $rdslist = Redis::get('clickreturn');
        if ($rdslist) {
            return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
        }
        Redis::setex('clickreturn', 2, 1);
        $data = input('get.');
        $res = (new AreaPrice())->where('id', $data['id'])->delete();
        if ($res) {
            return json(['status' => 0, 'data' => [], 'msg' => '操作完成']);
        }
        return json(['status' => 1, 'data' => [], 'msg' => '操作失败']);
    }
}